package com.twu.biblioteca;

import java.io.*;
import java.util.List;
import java.util.Map;

public class Biblioteca {
    private IOManager ioManager;
    private String welcomeMessage;
    private Library library;
    private List<LibraryBook> bookList;
    private Map<String, LibraryAction> options;

    //Default Actions
    private String LIST_BOOKS = "1";


    public Biblioteca(IOManager ioManager, String welcomeMessage, Library library, Map<String, LibraryAction> options) {
        this.ioManager = ioManager;
        this.welcomeMessage = welcomeMessage;
        this.library = library;
        this.options = options;
    }

    public void run(){
        displayWelcomeMessage();
        options.get(LIST_BOOKS).performAction();
        displayOptions();
        try {
            executeUserChoice(getUserOptionChoice());
        } catch (IOException e) {
            ioManager.print("Error Reading Input");
        }
    }

    public void displayWelcomeMessage() {
        ioManager.print(welcomeMessage);
    }

    public void displayOptions() {
        for(LibraryAction option : options.values())
            ioManager.print(option.toString());
    }

    public String getUserOptionChoice() throws IOException {
        return ioManager.getUserInput();
    }

    public void executeUserChoice(String userChoice) throws IOException {
        if(!isValid(userChoice)){
            ioManager.print("Select a valid option!");
            getUserOptionChoice();
        }
        else{
            options.get(userChoice).performAction();
        }
    }

    private boolean isValid(String userChoice) {
        if (options.containsKey(userChoice))
            return true;
        return false;
    }

}
